<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>角色管理</title>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">学生名</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="请输入学生名/学号/手机号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="button" class="layui-btn" id="test3"><i class="layui-icon"></i>导入学生</button>
                </div>
            </div>

        </div>
    </div>

    <div class="layui-card-body">
        <table class="layui-hide" id="demo" lay-filter="test"></table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            </div>
        </script>
    </div>
</div>
</div>


<script type="text/javascript" src="<%=request.getContextPath()%>/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '<%=request.getContextPath()%>/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('index');
    layui.use(['layer','form', 'table', 'element', 'upload'], function(){
        var layer = layui.layer //弹层
            ,form = layui.form
            ,table = layui.table //表格
            ,upload = layui.upload //表格
            ,element = layui.element //元素操作
            ,$ = layui.$
        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('demo', {
                where: field
            });
        });
        //指定允许上传的文件类型
        upload.render({
            elem: '#test3'
            ,url:'<%=request.getContextPath()%>/student/uploadStudents' //数据接口
            ,accept: 'file' //普通文件
            ,done: function(res){
                console.log(res);
                if(res=="0"){
                    layer.msg('导入成功');
                    location.reload();
                }else{
                    layer.msg(res.data, {
                        time: 0 //不自动关闭
                        ,btn: ['确定']
                        ,yes: function(index){
                            location.reload();
                        }
                    });
                }

            }
        });
        //执行一个 table 实例
        table.render({
            elem: '#demo'
            ,url: '<%=request.getContextPath()%>/student/list' //数据接口
            ,toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cols: [[ //表头
                {type:'numbers',width:80, title: '序号',fixed: 'center',align: "center"}
                ,{field: 'id', title: 'ID', width:130, align:'center', totalRowText: '合计：'}
                ,{field: 'name', title: '姓名',align:'center'}
                ,{field: 'studentNo', title: '学号', align:'center'}
                ,{field: 'gender', title: '性别',align:'center'}
                ,{field: 'moblephone', title: '手机号',align:'center'}
                ,{field: 'eMail', title: '邮箱', align:'center'}
                ,{fixed: 'right',  align:'center', toolbar: '#barDemo'}
            ]]
            ,page: true //开启分页
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2,
                        title: '添加学生',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['600px', '580px'],
                        content: '<%=request.getContextPath()%>/student/toAdd' //iframe的url
                    });
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
          if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        type: "post",
                        url: "<%=request.getContextPath()%>/student/del",
                        data: {"id":data.id},
                        dataType: "json",
                        success: function (date) {
                        }
                    });
                    layer.msg('删除成功');
                    obj.del(); //删除对应行（tr）的DOM结构
                });
            } else if(layEvent === 'edit'){
                layer.open({
                    type: 2,
                    title: '修改学生',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['600px', '580px'],
                    content: '<%=request.getContextPath()%>/student/toUpdate?id='+data.id //iframe的url
                });
            }
        });

    });

</script>
</body>
</html>
